﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace VietKinsNutrition.Data
{
    public class ClsTblDm_NhucaudinhduongotruongDAL : ClsConnection
    {


        public DataTable GetTable(string proName, bool Vungmien)
        {
            OleDbConnection cn = ClsConnection.OleDBConnect();
            OleDbCommand comand = new OleDbCommand(proName, cn);
            //comand.CommandType = CommandType.StoredProcedure;
            if (proName.ToString() == "PrcTblDm_Nhucaudinhduongotruong_SelecAll" || proName.ToString() == "PrcTblDm_Nhucaudinhduongotruong_Get")
            {
                comand.Parameters.Add(new OleDbParameter("@Vungmien", Vungmien));
            }
            DataTable table = new DataTable();

            DataColumn colum = new DataColumn();
            colum.ColumnName = colum.Caption = "STT";
            colum.DataType = System.Type.GetType("System.Int32");
            colum.AllowDBNull = false;
            colum.AutoIncrement = true;
            colum.AutoIncrementSeed = colum.AutoIncrementStep = 1;
            table.Columns.Add(colum);

            try
            {
                //  comand.Parameters.Add("@Option", OleDbType.Integer).Value = Option;
                //  comand.Parameters.Add("@Id", OleDbType.Integer).Value = Id;
                comand.ExecuteNonQuery();
                OleDbDataAdapter Adapter = new OleDbDataAdapter(comand);
                Adapter.Fill(table);
                cn.Close();
            }
            catch (System.Exception ex)
            { MessageBox.Show("Error " + ex.Message); }
            return table;
        }

        public void InsertData(string proName, string[] Value, float[] Values, bool Vungmien, int Id)
        {
            int iVungmien;
            if (Vungmien == true)
            {
                iVungmien = -1;
            }
            else iVungmien = 0;
            OleDbConnection cnn = ClsConnection.OleDBConnect();
            OleDbCommand comand = new OleDbCommand();
            if (proName.ToString() == "PrcTblDm_Nhucaudinhduongotruong_Insert")
            {
                //comand.CommandType = CommandType.StoredProcedure;
                proName = "INSERT INTO TblDm_Nhucaudinhduongotruong (KhoilopId,KCaloCd, KCaloCtr, ProtidCd, ProtidCtr, LipidCd, LipidCtr, GlucidCd, GlucidCtr,Vungmien) VALUES     (@KhoilopId,@KCaloCd,@KCaloCtr,@ProtidCd,@ProtidCtr,@LipidCd,@LipidCtr,@GlucidCd,@GlucidCtr,@Vungmien)";
                comand.Connection = cnn;

                try
                {

                    //  comand.Parameters.Add(Value[i], OleDbType.Double).Value = TG;
                    proName = proName.Replace("@KCaloCd", Values[1].ToString()).Replace("@KCaloCtr", Values[2].ToString()).Replace("@ProtidCd", Values[3].ToString()).Replace("@ProtidCtr", Values[4].ToString()).Replace("@LipidCd", Values[5].ToString()).Replace("@LipidCtr", Values[6].ToString()).Replace("@GlucidCd", Values[7].ToString()).Replace("@GlucidCtr", Values[8].ToString()).Replace("@KhoilopId", Values[0].ToString()).Replace("@Vungmien", iVungmien.ToString());
                    comand.CommandText = proName;
                    comand.ExecuteNonQuery();
                    cnn.Close();
                }
                catch (System.Exception ex)
                { MessageBox.Show("Error proc " + ex.Message); }

            }
            else
            {
                try
                {
                    string StrCommand = proName.Replace("@KCaloCd", Values[1].ToString()).Replace("@KCaloCtr", Values[2].ToString()).Replace("@ProtidCd", Values[3].ToString()).Replace("@ProtidCtr", Values[4].ToString()).Replace("@LipidCd", Values[5].ToString()).Replace("@LipidCtr", Values[6].ToString()).Replace("@GlucidCd", Values[7].ToString()).Replace("@GlucidCtr", Values[8].ToString()).Replace("@Id", Id.ToString());
                    comand.Connection = cnn;
                    comand.CommandText = StrCommand;
                    comand.ExecuteNonQuery();
                    cnn.Close();
                }
                catch (System.Exception ex)
                { MessageBox.Show("Error proc " + ex.Message); }

            }

        }
    }
}
